package com.onegroup.common.handler;

import com.alibaba.fastjson2.JSON;
import com.onegroup.common.enums.ResponseStatusEnum;
import com.onegroup.common.utils.WebUtil;
import com.onegroup.pojo.ResponseResult;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author: 曾雪斌
 * @Description: 对权限认证失败的处理
 * @History:
 */
@Component
public class AccessDeniedHandlerImpl implements AccessDeniedHandler {

    /**
     * 对权限认证失败的处理
     * @param request 请求
     * @param response 响应
     * @param accessDeniedException 权限拒绝异常
     * @throws IOException IO异常
     * @throws ServletException Servlet异常
     */
    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException)
            throws IOException, ServletException {
        ResponseResult<String> result = new ResponseResult<>(ResponseStatusEnum.ROLE_ERROR);
        String resultJson = JSON.toJSONString(result);
        WebUtil.renderString(response, resultJson);
    }
}
